<template>
  <div class="wisdomContainer">
    <!-- 头部 -->
    <van-nav-bar
      title="管理助手"
      left-arrow
      @click-left="onClickLeft"
      class="navheadrt"
    >
    </van-nav-bar>
    <div class="bookIndexContainer">
      <div class="searchCont f">
        <input
          type="text"
          placeholder="请输入关键字"
          class="searchInput fl"
          v-model="keywords"
        />
        <div class="right f fc" @click="toSearch">
          <van-icon name="search" class="searchicon" />
          搜索
        </div>
      </div>

      <!--tab标签-->
      <ul class="f fb tabul">
        <li
          class="tabli"
          v-for="(item, index) in tabNav"
          :key="index"
          @click="tabTap(index, item)"
        >
          <span class="title" :class="{ titleAct: tabIndex == index }">{{
            item.name
          }}</span>
        </li>
      </ul>
      <!--内容-->
      <div class="bookContent">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          :error.sync="error"
          error-text="请求失败，点击重新加载"
          class="my-list"
          @load="GetDocumentPageList1"
        >
          <ul class="cont_ul f fs vfs" v-if="booklist.length > 0">
            <li
              class="cont_li"
              v-for="(item, index) in booklist"
              :key="index"
              @click="tobookContent(item.ID)"
            >
              <div class="top f fc">
                <img
                  :src="item.Cover != null ? item.Cover : bookimg"
                  alt=""
                  class="bookimg"
                />
              </div>
              <div class="bot">
                <p class="title">{{ item.DocumentName }}</p>
                <p class="smallT">{{ item.ReadCount }}人学过</p>
              </div>
            </li>
          </ul>
          <div v-else class="nodata"><img :src="nodata" alt="" /></div>
        </van-list>
      </div>
    </div>
  </div>
</template>
<script>
import {
  GetDictInfoList,
  GetDocumentPageList,
  postreadList,
  getbookName,
} from "@/api/api";
var projectId = localStorage.getItem("projectid");
var userId = localStorage.getItem("userId");
export default {
  data() {
    return {
      nodata: require("../../imgs/nodata.png"),
      bookimg: require("../../imgs/13.png"),
      tabIndex: 0,
      tabNav: [{ name: "浏览记录" }],
      booklist: [],
      paging: { page: 1, rows: 12 },
      category: "",
      keywords: "",
      total: "",
      loading: false, //是否处于加载状态
      finished: false, //是否已加载完成
      error: false,
      DocumentId: "", //书籍id
    };
  },
  mounted() {
    this.GetDictInfoList1();
    // this.GetDocumentPageList1();
  },
  methods: {
    handleNodeClick(data) {
      this.drawer = false;
    },
    tobookContent(id) {
      this.DocumentId = id;
      this.postreadList1();
      this.$router.push({
        path: "/bookContent",
        query: {
          id,
        },
      });
    },
    toBook() {
      this.$router.push({ path: "/bookIndex" });
    },
    toSearch() {
      this.booklist = [];
      this.paging.page = 1;
      this.GetDocumentPageList1();
    },

    onClickLeft() {
      this.$router.push({ path: "/nav/to/wisdom-site" });
    },
    tabTap(index, item) {
      this.tabIndex = index;
      if (item.name == "浏览记录") {
        this.category = "";
        this.booklist = [];
        this.paging.page = 1;
        this.GetDocumentPageList1();
      } else {
        this.category = item.name;
        this.booklist = [];
        this.paging.page = 1;
        this.GetDocumentPageList1();
      }
    },
    //获取头部nav
    GetDictInfoList1() {
      this.Request(
        GetDictInfoList({ DICode: "文档类型"})
      ).then((res) => {
        if (res.StatusCode == "200") {
          var arr = res.Detiel;
          arr.forEach((obj) => {
            var obj1 = {};
            obj1.name = obj.cDIName;
            this.tabNav.push(obj1);
          });
        } else {
          this.$message({
            type: "error",
            message: res.Message,
            center: "true",
          });
        }
      });
    },
    //获取列表
    GetDocumentPageList1() {
      var that = this;
      that.$toast.loading({
        mask: true,
        message: "加载中...",
        className: "my-toast",
      });
      var params = {
        ProjectID: projectId,
        category: that.category,
        keywords: that.keywords,
        pageIndex: that.paging.page,
        pageSize: that.paging.rows,
      };
      that.Request(GetDocumentPageList(params)).then((res) => {
        if (res.StatusCode == "200") {
          that.$toast.clear();
          that.total = Number(res.Count);
          that.booklist.push(...res.Detiel);
          // 数据全部加载完成
          if (that.paging.page * that.paging.rows >= res.Count) {
            that.finished = true;
          } else {
            that.finished = false;
          }
          that.paging.page += 1;
        } else {
          that.$message({
            type: "error",
            message: res.Message,
            center: "true",
          });
        }
        this.loading = false;
      });
    },
    //添加阅读记录
    postreadList1() {
      var that = this;
      var params = {
        ProjectID: projectId,
        ReadUserId: userId,
        DocumentId: that.DocumentId,
      };
      this.Request(postreadList(params)).then((res) => {
        if (res.StatusCode == "200") {
          console.log("添加成功");
        } else {
          this.$message({
            type: "error",
            message: res.Message,
            center: "true",
          });
        }
      });
    },
  },
};
</script>
<style lang="stylus" scoped>
@import '~@/assets/css/resetVant.css';

.wisdomContainer {
  width: 100%;
  height: 100%;

  .nodata {
    width: 100%;
    height: 100%;
    margin: 100px 0;
    text-align: center;

    img {
      width: 60%;
    }
  }

  .navheadrt {
    /deep/ .van-nav-bar__title, /deep/.van-icon {
      color: #fff !important;
    }
  }

  .van-nav-bar {
    width: 100%;
    height: 88px;
    // border-bottom: 1px solid #E5E5E5;
    background: #699fff;

    .van-nav-bar__title, .van-nav-bar__left, .van-nav-bar__right {
      color: #fff;
      height: 87px;
      line-height: 87px;
      font-size: $font-size-title;
      z-index: 10000;
      border: none;
    }

    .van-icon-arrow-left {
      color: #fff;
      font-size: $font-size-title;
    }

    .van-nav-bar__text {
      color: #fff;
      font-size: $font-size-title;
    }
  }

  .bookIndexContainer {
    width: 100%;
    height: calc(100% - 88px);
    padding: 30px 40px;
    box-sizing: border-box;
    overflow: hidden;

    .searchCont {
      width: 100%;
      margin-bottom: 60px;

      .searchInput {
        height: 56px;
        background: #FFFFFF;
        box-shadow: 0px 5px 16px 0px rgba(70, 70, 70, 0.26);
        border-radius: 5px;
        padding-left: 30px;
        box-sizing: border-box;
        font-size: 28px;
      }

      .right {
        width: 120px;
        height: 56px;
        margin-left: 15px;
        background: #699FFF;
        box-shadow: 0px 5px 16px 0px rgba(70, 70, 70, 0.26);
        border-radius: 5px;
        font-size: 28px;
        font-family: Microsoft YaHei;
        font-weight: 300;
        color: #FFFFFF;

        .searchicon {
          font-size: 32px;
          margin-right: 15px;
        }
      }
    }

    .tabul {
      margin-bottom: 30px;

      .tabli:nth-child(2) {
        text-align: center;
      }

      .tabli:nth-child(3) {
        text-align: right;
      }

      .tabli {
        width: 30%;

        .title {
          width: 130px;
          font-size: 30px;
          font-family: Microsoft YaHei;
          font-weight: 300;
          color: #696969;
          padding-bottom: 10px;
        }

        .titleAct {
          border-bottom: 6px solid #699fff;
          color: #699fff !important;
        }
      }
    }

    .bookContent {
      width: 100%;
      height: calc(100% - 170px);
      // border: 1px solid #f00;
      overflow: auto;
      background: #fff;
      border-radius: 15px;

      .cont_ul {
        width: 100%;
        padding-top: 20px;
        flex-wrap: wrap;

        .cont_li:nth-child(3n + 3) {
          margin-right: 0;
        }

        .cont_li {
          width: 30%;
          margin-bottom: 25px;
          margin-right: 5%;

          .top {
            width: 100%;
            height: 263px;
            background: #D1D2D3;
            margin-bottom: 25px;

            .bookimg {
              width: 167px;
            }
          }

          .bot {
            width: 100%;

            .title {
              font-size: 24px;
              font-family: Microsoft YaHei;
              font-weight: 400;
              color: #011340;
              margin-bottom: 15px;
            }

            .smallT {
              font-size: 22px;
              font-family: Microsoft YaHei;
              font-weight: 300;
              color: #8E8E8E;
            }
          }
        }
      }
    }
  }

  .draweContent {
    width: 100%;
    padding: 30px;
    box-sizing: border-box;
    font-size: 28px;

    .tree1 {
      font-size: 28px;

      /deep/.el-tree-node__content {
        height: 50px;
      }

      /deep/.el-tree-node__label {
        font-size: 28px;
      }
    }
  }
}
</style>